package com.dailyblue.java.spring.project.smbms.commons.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.java.spring.project.smbms.commons.bean.Users;
import com.dailyblue.java.spring.project.smbms.commons.config.JsonResult;
import com.dailyblue.java.spring.project.smbms.commons.config.JwtConfig;
import com.dailyblue.java.spring.project.smbms.commons.config.ResultCode;
import com.dailyblue.java.spring.project.smbms.commons.config.ResultTool;
import com.dailyblue.java.spring.project.smbms.commons.mapper.UsersMapper;
import com.dailyblue.java.spring.project.smbms.commons.service.UsersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper,Users> implements UsersService {
    @Resource
    private UsersMapper usersMapper;
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    public JsonResult<String> login(Users user) {
        user = usersMapper.login(user);
        if (user == null) {
            return ResultTool.fail(ResultCode.USER_CREDENTIALS_ERROR);
        }
        int gender = user.getGender();
        if (gender == 1) {
            user.setUserSex("女");
        } else {
            user.setUserSex("男");
        }
        user.setUserPassword(null);
        // 生成 jwt 字符串
        String jwt = JwtConfig.getJwtToken(user);
        // 需要将 jwt 存放到 Redis
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setValueSerializer(stringSerializer);
        ValueOperations<String, String> value = redisTemplate.opsForValue();
        value.set(("token:" + user.getId()).trim(), jwt);
        return ResultTool.success(jwt);
    }

    @Cacheable(cacheNames = "findAllUsers", key = "#page")
    public JsonResult<PageInfo<Users>> findAllUsers(int page) {
        PageHelper.startPage(page, 5);
        List<Users> list = usersMapper.findAllUser();
        PageInfo<Users> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }

    @Cacheable(cacheNames = "findUserByNameAndRoleName", key = "#name+'.'+#role+'.'+#page")
    public JsonResult<PageInfo<Users>> findUserByNameAndRoleName(String name, Integer role, Integer page) {
        PageHelper.startPage(page, 5);
        List<Users> list = usersMapper.findUserByNameAndRoleName(name, role);
        PageInfo<Users> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }
    @CachePut(cacheNames = "findAllUsers",key = "#page")
    public JsonResult<PageInfo<Users>> delete(QueryWrapper<Users> wrapper, int page){
        usersMapper.delete(wrapper);
        return findAllUsers(page);
    }
    @CachePut(cacheNames = "findAllUsers",key = "#page")
    public JsonResult<PageInfo<Users>> delete(int page){
        return findAllUsers(page);
    }
}
